package com.central.scheduler.service.quartz.cron;

import com.central.scheduler.common.enums.CycleEnum;
import com.cronutils.model.Cron;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tindy
 * @date 2021/11/5
 * @describe
 * DAG Cycle judge
 */
public class CycleLinks extends AbstractCycle {
    private final List<AbstractCycle> cycleList = new ArrayList<>();

    public CycleLinks(Cron cron) {
        super(cron);
    }

    /**
     * add cycle
     * @param cycle cycle
     * @return CycleLinks
     */
    @Override
    public CycleLinks addCycle(AbstractCycle cycle) {
        cycleList.add(cycle);
        return this;
    }

    /**
     * get cycle
     * @return CycleEnum
     */
    @Override
    protected CycleEnum getCycle() {
        for (AbstractCycle abstractCycle : cycleList) {
            CycleEnum cycle = abstractCycle.getCycle();
            if (cycle != null) {
                return cycle;
            }
        }

        return null;
    }

    /**
     * get mini cycle
     * @return CycleEnum
     */
    @Override
    protected CycleEnum getMiniCycle() {
        for (AbstractCycle cycleHelper : cycleList) {
            CycleEnum cycle = cycleHelper.getMiniCycle();
            if (cycle != null) {
                return cycle;
            }
        }

        return null;
    }

}
